package com.topmap.controller;

import com.topmap.controller.viewobject.FeiyiVO;
import com.topmap.dao.FeiyiDOMapper;
import com.topmap.response.CommonReturnType;
import com.topmap.service.FeiyiService;
import com.topmap.service.model.FeiyiModel;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: MaHuadong
 * @Date: 2019/7/3 10:56
 * @Version 1.0
 */
@Controller("/feiyi")
@RequestMapping("/feiyi")
//允许跨域
@CrossOrigin(origins = {"*"},allowCredentials = "true")

public class FeiyiController {

    @Autowired
    private FeiyiService feiyiService;

    @Autowired
    private FeiyiDOMapper feiyiDOMapper;

    //通过非遗名称查询非遗
    @RequestMapping(value = "/getTitle",method = {RequestMethod.GET})
    @ResponseBody
    public CommonReturnType getFeiyi(@RequestParam(name = "title") String title){
        //首先根据非遗名称获取非遗model
        FeiyiModel feiyiModel=feiyiService.getFeiyiByTitle(title);

        //把model转换为供前端展示的VO层
        FeiyiVO feiyiVO=convertVOFromModel(feiyiModel);

        return CommonReturnType.creat(feiyiVO);
    }

    //通过非遗id查询非遗
    @RequestMapping(value = "/getId",method = {RequestMethod.GET})
    @ResponseBody
    public CommonReturnType getFeiyiById(@RequestParam(name = "id") Integer id){
        //首先根据非遗名称获取非遗model
        FeiyiModel feiyiModel=feiyiService.getFeiyiById(id);

        //把model转换为供前端展示的VO层
        FeiyiVO feiyiVO=convertVOFromModel(feiyiModel);

        return CommonReturnType.creat(feiyiVO);
    }

    //通过非遗类型查询非遗
    @RequestMapping(value = "/getType",method = {RequestMethod.GET})
    @ResponseBody
    public CommonReturnType getFeiyiByType(@RequestParam(name = "type") String type){
        //首先根据非遗名称获取非遗model
        List<FeiyiModel> feiyiModelList=feiyiService.getFeiyiByType(type);

        //把model转换为供前端展示的vo(涉及到list类型数据的转换)还是使用java8的stream的api
        List<FeiyiVO> feiyiVOList=feiyiModelList.stream().map(feiyiModel -> {
            FeiyiVO feiyiVO=this.convertVOFromModel(feiyiModel);
            return feiyiVO;
            //转换为list类型，使得可以返回list类型的数据
        }).collect(Collectors.toList());

        //最后将返回给前端的productvo返回给前端
        return CommonReturnType.creat(feiyiVOList);
    }

    //查询所有的非遗
    @RequestMapping(value = "/listfeiyi",method = {RequestMethod.GET})
    @ResponseBody
    public CommonReturnType getAllFeiyi(){
        List<FeiyiModel> feiyiModelList=feiyiService.listFeiyi();

        //把model转换为供前端展示的vo(涉及到list类型数据的转换)还是使用java8的stream的api
        List<FeiyiVO> feiyiVOList=feiyiModelList.stream().map(feiyiModel -> {
            FeiyiVO feiyiVO=this.convertVOFromModel(feiyiModel);
            return feiyiVO;
            //转换为list类型，使得可以返回list类型的数据
        }).collect(Collectors.toList());

        //最后将返回给前端的productvo返回给前端
        return CommonReturnType.creat(feiyiVOList);
    }



    //将model转换为VO返回给前端展示
    private FeiyiVO convertVOFromModel(FeiyiModel feiyiModel) {
        if (feiyiModel==null){
            return null;
        }

        FeiyiVO feiyiVO=new FeiyiVO();

        //copy
        BeanUtils.copyProperties(feiyiModel,feiyiVO);

        return feiyiVO;
    }


}
